Skip to content

[CPU] Fix BNNS GEMM descriptor dimensions for strided half-precision - #4924

Closed
Yunt1 wants to merge 2 commits into
ml-explore:mainfrom
Yunt1:fix/4739-cpu-segmented-mm-half
Closed

Yunt1 wants to merge 2 commits into
ml-explore:mainfrom
Yunt1:fix/4739-cpu-segmented-mm-half

Conversation

@Yunt1

@Yunt1 Yunt1 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor
  • ☑️ I understand it is strictly prohibited to use AI to write PR description

  • AI usage disclosure:
    Fixes CPU segmented_mm gives wrong values for bfloat16 and float16 #4739.

    Summary

    • In matmul_bnns (mlx/backend/cpu/gemms/bnns.cpp), BNNSNDArrayDescriptor::size for BNNSDataLayoutRowMajorMatrix was set to {lda, (M * K) / lda} and {ldb, (K * N) / ldb} instead of the actual logical matrix dimensions {cols, rows}.
    • When segmented_mm (or any strided slice) invokes matmul<float16_t> / matmul<bfloat16_t> with K < lda or N < ldb, (M * K) / lda truncates to the wrong
      row count (or 0) and reads with the wrong column extent, producing incorrect results.
    • Set iA_desc.size to {a_transposed ? M : K, a_transposed ? K : M}, iB_desc.size to {b_transposed ? K : N, b_transposed ? N : K}, and o_desc.stride to
      {1, ldc} while keeping {1, lda} / {1, ldb} as the leading-dimension strides.
    • Add a C++ regression test (test segmented_mm float16 and bfloat16 in tests/blas_tests.cpp) covering all four transpose combinations for both float16 and
      bfloat16.

    Testing

    Built with -DMLX_BUILD_METAL=OFF -DMLX_BUILD_TESTS=ON on macOS arm64:

    • ./build/tests/tests -sf="*blas_tests.cpp": 2/2 test cases passed (31/31 assertions)
    • ./build/tests/tests: 277/277 test cases passed (4442/4442 assertions)

@Yunt1 Yunt1 changed the title [CPU] Fix BNNS GEMM descriptor dimensions for strided half-precision inputs [CPU] Fix BNNS GEMM descriptor dimensions for strided half-precision Nov 20, 2026
@zcbenz
zcbenz marked this pull request as ready for review November 22, 2026 23:45
@zcbenz
zcbenz force-pushed the fix/4739-cpu-segmented-mm-half branch from 3ec5f01 to 6ac4a0a Compare November 22, 2026 23:45
@zcbenz
zcbenz force-pushed the fix/4739-cpu-segmented-mm-half branch from 6ac4a0a to 4f4b3b1 Compare November 23, 2026 01:36

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are failing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CPU segmented_mm gives wrong values for bfloat16 and float16

2 participants